package gu.sql2java.guava;

import static gu.sql2java.guava.DeepEquivalence.DEEP_EQUIVALENCE;

import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentMap;

import com.google.common.base.Equivalence;
import com.google.common.base.Throwables;
import com.google.common.collect.MapMaker;

/**
 * 
 * @author guyadong
 *
 */
public class MapBuilder {

	/**
	 * 创建支持Object[]为key的map
	 */
	public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
		
		try {
			// 等价于 new MapMaker().keyEquivalence(DEEP_EQUIVALENCE).<K, V>makeMap();
			Method method = MapMaker.class.getDeclaredMethod("keyEquivalence", Equivalence.class);
			method.setAccessible(true);
			MapMaker mapMaker= (MapMaker) method.invoke(new MapMaker(), DEEP_EQUIVALENCE);
			return mapMaker.<K, V>makeMap();
		} catch (Exception e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
}
